package cn.zhangxiaohang.Utils;

import org.apache.http.HttpResponse;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.utils.HttpClientUtils;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;

import java.io.Closeable;
import java.io.IOException;
import java.util.Map;
import java.util.Set;

public class HttpRequestUtil {
    //    获取httpClient
    public static CloseableHttpClient getHttpclient(){
        return HttpClients.createDefault();
    }

    /*
     * 发送get请求
     * 步骤：httpClient
     *       请求路径，
     *        请求头
     *        请求参数
     * */
    public static HttpResponse sendGetRequestGetRes(CloseableHttpClient httpClient,String url) throws IOException {
        HttpGet httpGet = new HttpGet(url);
//        下面请求配置
        RequestConfig requestConfig = RequestConfig.custom().setConnectTimeout(3000) //连接url超时时间
                .setSocketTimeout(3000).build(); //获取响应数据超时时间
        httpGet.setConfig(requestConfig);
        httpGet.setHeader("user-agent","Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/103.0.5060.134 Safari/537.36 Edg/103.0.1264.71");
//        httpGet.setHeader("Connection","keep-alive");
        HttpResponse response = null;
        response = httpClient.execute(httpGet);
        return response;
    }
    public static HttpResponse sendGetRequestGetRes(CloseableHttpClient httpClient,String url,Map<String,String> header) throws IOException {
        HttpGet httpGet = new HttpGet(url);
//        下面请求配置
        RequestConfig requestConfig = RequestConfig.custom().setConnectTimeout(3000) //连接url超时时间
                .setSocketTimeout(3000).build(); //获取响应数据超时时间
        httpGet.setConfig(requestConfig);
        httpGet.setHeader("user-agent","Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/103.0.5060.134 Safari/537.36 Edg/103.0.1264.71");
        Set<Map.Entry<String, String>> entries = header.entrySet();
        for (Map.Entry<String, String> entry : entries) {
            httpGet.setHeader(entry.getKey(), entry.getValue());
        }
//        httpGet.setHeader("Connection","keep-alive");
        HttpResponse response = null;
        response = httpClient.execute(httpGet);
        return response;
    }
    /*
     * 将response数据进行转换，字符串，二进制
     * */
    public static  String getStringByResponse(HttpResponse httpResponse) throws IOException {
        String result = null;
        result = EntityUtils.toString(httpResponse.getEntity(), "utf-8");
        return result;
    }

    /*
     * 关闭httpClient
     * */
    public static void closeHttpClient(CloseableHttpClient httpClient,HttpResponse httpResponse){
        HttpClientUtils.closeQuietly(httpClient);
        HttpClientUtils.closeQuietly(httpResponse);
    }
    /*
     * 关闭io流
     * */
    public static void closeIO(Closeable Closeable){
        if(Closeable!=null)
        {
            try {
                Closeable.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    /*
     * 发起一个get请求获取响应体字符串
     * */
    public static String getStringByGetRequest(String url){
        CloseableHttpClient httpclient = getHttpclient();
        HttpResponse httpResponse = null;
        String response = null;
        try {
            httpResponse = HttpRequestUtil.sendGetRequestGetRes(httpclient, url);
            response = HttpRequestUtil.getStringByResponse(httpResponse);

        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            HttpRequestUtil.closeHttpClient(httpclient,httpResponse);
        }
        return response;
    }
    public static String getStringByGetRequest(String url, Map<String,String> header){
        CloseableHttpClient httpclient = getHttpclient();
        HttpResponse httpResponse = null;
        String response = null;
        try {
            httpResponse = HttpRequestUtil.sendGetRequestGetRes(httpclient, url,header);
            response = HttpRequestUtil.getStringByResponse(httpResponse);

        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            HttpRequestUtil.closeHttpClient(httpclient,httpResponse);
        }
        return response;
    }
}
